﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System.Collections.ObjectModel;
using ZO.SmartCore.Xml;

#endregion

namespace ZO.SmartCore.Protocols.XMPP.SASL.Core
{
    /// <summary>
    /// SASL BaseMechanism
    /// </summary>
    public class Mechanisms : Element
    {

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="Mechanisms"/> class.
        /// </summary>
        public Mechanisms() : base("mechanisms", "", UriList.SASL) { } 
        #endregion

        #region Destructor

        #endregion

        #region Fields

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>
        /// Gets all the mechanisms.
        /// </summary>
        /// <value>The mechanisms.</value>
        public ReadOnlyCollection<Mechanism> AllMechanisms
        {
            get
            {
                Collection<Mechanism> mechanismList = new Collection<Mechanism>();
                ReadOnlyCollection<Element> list = this.SelectElements("mechanism");

                foreach (Element currentElement in list)
                {
                    mechanismList.Add(new Mechanism(currentElement.Value));
                } // foreach 

                return new ReadOnlyCollection<Mechanism>(mechanismList);
            }
        } // GetMechanisms    
        #endregion

        #region Methods
        /// <summary>Adds the specified BaseMechanism to the end of the list of this Mechanisms.</summary>
        /// <param name="newMechanism">The BaseMechanism to add</param>
        public virtual void AddMechanism(Mechanism newMechanism)
        {
            this.AddChild(newMechanism);
        }


        /// <summary>
        /// Adds the specified BaseMechanism to the end of the list of this Mechanisms.
        /// </summary>
        /// <param name="type">The type.</param>
        public virtual void AddMechanism(MechanismType type)
        {

            if (type == MechanismType.None)
                return;


            this.AddChild(new Mechanism(type));
        }

        /// <summary>Remove the specified BaseMechanism from the list of child nodes, of this Mechanisms.</summary>
        /// <param name="newMechanism">The BaseMechanism to Remove</param>
        /// <remarks>it only removes child of current BaseMechanism</remarks>
        public virtual void RemoveElement(Mechanism newMechanism)
        {
            this.RemoveChild(newMechanism);
        }


        /// <summary>
        /// Get whether this mechanism collection supports specified type
        /// </summary>
        /// <param name="type">The mechanism type.</param>
        public bool SupportsMechanism(MechanismType type)
        {
            foreach (Mechanism mechanism in this.AllMechanisms)
            {
                if (mechanism.Type == type)
                {
                    return true;
                }
            }
            return false;
        }

 


        #endregion
    }
}
